package com.mindcraft.user.api;


import com.mindcraft.base.model.MindCraftResult;
import com.mindcraft.base.utils.CommonConst;
import com.mindcraft.user.Dto.LoginDto;
import com.mindcraft.user.po.McUser;
import com.mindcraft.user.serivce.UserService;
import com.mindcraft.user.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Api(value = "user", tags = "用户管理接口")
public class UserController {

    @Autowired
    private UserService userService;


    @Autowired
    private RedisTemplate redisTemplate;



    /**
     * 用户名、邮箱、手机号/密码登录
     */
    @ApiOperation("用户名、邮箱、手机号/密码登录")
    @PostMapping("/login")
    public MindCraftResult<UserVO> login(@RequestBody LoginDto loginDto) {
        return userService.login(loginDto);
    }


    /**
     * Token登录
     */
    @ApiOperation("Token登录")
    @PostMapping("/token")
    public MindCraftResult<UserVO> login(@RequestParam("userToken") String userToken) {
        return userService.token(userToken);
    }


    /**
     * 退出
     */
    @GetMapping("/logout")
    @ApiOperation("退出")
    public MindCraftResult exit(@RequestHeader("Authorization") String usertoken) {
        return userService.exit(usertoken);
    }




    @ApiOperation("查询用户根据id")
    @GetMapping("getUser")
    public McUser getUser(@RequestParam("userId")String userId) {

        McUser user = (McUser) redisTemplate.opsForValue().get(CommonConst.USER_CACHE + userId.toString());
        if (user != null) {
            return user;
        }
        McUser u = userService.getById(userId);
        if (u != null) {
            redisTemplate.opsForValue().set(CommonConst.USER_CACHE + userId.toString(), u, CommonConst.EXPIRE, TimeUnit.SECONDS);
            return u;
        }
        return null;
    }

//    @ApiOperation("根据用户id列表查询用户")
//    @GetMapping("getUserByIds")
//    public List<User> getUserList(@RequestParam List<Integer> userIdList){
//        List<User> userList = userService.listByIds(userIdList);
//        return userList;
//    }



}
